using System;
using System.Runtime.Serialization;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Pegasus.UnitTests.Runtime.Serialization.Formatters.Xml.XmlFormatter2Tests
{
	/// <summary>
	/// This is a test class for Pegasus.Runtime.Serialization.Formatters.Xml.XmlFormatter2
	/// and will check the serialization of Value types.
	///</summary>
	[TestClass]
	public class XmlFormatter2ValueTypesTests
	{
		// Local Instance Values
		private TestContext m_testContextInstance;

		/// <summary>
		/// Initializes a new instance of the <see cref="T:XmlFormatter2ValueTypesTests"/> class.
		/// </summary>
		public XmlFormatter2ValueTypesTests()
		{
		}

		/// <summary>
		/// Gets or sets the test context which provides information about and 
		/// functionality for the current test run.
		/// </summary>
		/// <value>The test context.</value>
		public TestContext TestContext
		{
			get
			{
				return m_testContextInstance;
			}
			set
			{
				m_testContextInstance = value;
			}
		}

		/// <summary>
		/// Test the Serialize of TimSpan type.
		/// </summary>
		[TestMethod]
		public void SerializeTimeSpanTest()
		{
			long interval = ( TimeSpan.MaxValue.Ticks / ( XmlFormatter2Helper.TestLoops + 1 ) ) * 2;
			TimeSpan value = TimeSpan.MinValue;

			for( long x = 0; x < XmlFormatter2Helper.TestLoops; x++ )
			{
				Assert.AreEqual( (TimeSpan) XmlFormatter2Helper.SerializeDeserialize( value ), value, "TimeSpan {0} did not serialize correctly", value );
				value = new TimeSpan( value.Ticks + interval );
			}
		}

		/// <summary>
		/// Test the Serialize of DateTime type.
		/// </summary>
		[TestMethod]
		public void SerializeDateTimeTest()
		{
			TimeSpan interval = ( DateTime.MaxValue - DateTime.MinValue );
			interval = new TimeSpan( interval.Ticks / XmlFormatter2Helper.TestLoops );
			DateTime value = DateTime.MinValue;

			for( long x = 0; x < XmlFormatter2Helper.TestLoops; x++ )
			{
				Assert.AreEqual( (DateTime) XmlFormatter2Helper.SerializeDeserialize( value ), value, "DateTime {0} did not serialize correctly", value );
				value += interval;
			}
		}

		/// <summary>
		/// Test the Serialize of Guid type.
		/// </summary>
		[TestMethod]
		public void SerializeGuidTest()
		{
			for( long x = 0; x < XmlFormatter2Helper.TestLoops; x++ )
			{
				Guid guid = Guid.NewGuid();
				Assert.AreEqual( (Guid) XmlFormatter2Helper.SerializeDeserialize( guid ), guid, "Guid {0} did not serialize correctly", guid );
			}
		}

		/// <summary>
		/// Test the Serialize method (basic structure object)
		/// </summary>
		[TestMethod]
		public void SerializeStructTest1()
		{
			// Build the object to serialize 
			SerializationStruct startObj = new SerializationStruct();
			startObj.Name = XmlFormatter2Helper.StringTestValue;
			startObj.Description = XmlFormatter2Helper.StringTestValue;
			startObj.Count = XmlFormatter2Helper.IntTestValue;

			// Send the object through the serializtion and deserializtion process
			SerializationStruct finishedObj = (SerializationStruct) XmlFormatter2Helper.SerializeDeserialize( startObj );

			// Check test results
			Assert.AreEqual( finishedObj.Name, XmlFormatter2Helper.StringTestValue, "Name value is not right" );
			Assert.AreEqual( finishedObj.Description, XmlFormatter2Helper.StringTestValue, "Description value is not right" );
			Assert.AreEqual( finishedObj.Count, XmlFormatter2Helper.IntTestValue, "Count value is not right" );
		}

		/// <summary>
		/// Test the Serialize method (basic structure object)
		/// </summary>
		[TestMethod]
		[ExpectedException( typeof( SerializationException ), "Expected SerializeException" )]
		public void SerializeStructTest2()
		{
			// Build the object to serialize 
			NonserializationStruct startObj = new NonserializationStruct();
			startObj.Name = XmlFormatter2Helper.StringTestValue;
			startObj.Description = XmlFormatter2Helper.StringTestValue;
			startObj.Count = XmlFormatter2Helper.IntTestValue;

			// Send the object through the serializtion and deserializtion process
			NonserializationStruct finishedObj = (NonserializationStruct) XmlFormatter2Helper.SerializeDeserialize( startObj );
		}

	}
}
